package org.jeecg.cn.edu.scau.vm.node.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.cn.edu.scau.vm.node.domain.VmNode;
import org.jeecg.cn.edu.scau.vm.node.service.NodeService;
import org.jeecg.cn.edu.scau.vm.node.vo.*;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RequestMapping("/vm/vmNode")
@RestController
public class VmNodeController {
    @Resource
    private NodeService vmNodeService;

    public static final String EMPTY_INFO = "%s不得为空!";


    /**
     * 分页列表查询
     *
     * @param vmNode   查询条件
     * @param pageNo   分页页码
     * @param pageSize 页大小
     * @param req      请求对象
     * @return 分页查询列表
     */
    @ApiOperation(value = "节点分页查询", notes = "节点分页查询")
    @GetMapping(value = "/list")
    public Result<IPage<VmNodeListVo>> queryPageList(VmNode vmNode,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<VmNode> queryWrapper = QueryGenerator.initQueryWrapper(vmNode, req.getParameterMap());
        Page<VmNode> page = new Page<>(pageNo, pageSize);
        IPage<VmNodeListVo> pageList = vmNodeService.pageList(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加节点
     *
     * @param vmNode 节点实例
     * @return 添加节点提示信息
     */
    @ApiOperation(value = "添加节点", notes = "添加节点")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody VmNode vmNode) {
        /*校验参数*/
        if (Objects.isNull(vmNode)) {
            return Result.error(String.format(EMPTY_INFO, "入参"));
        }
        if (Objects.isNull(vmNode.getNodeIpAddress())) {
            return Result.error(String.format(EMPTY_INFO, "ip地址"));
        }
        if (Objects.isNull(vmNode.getPassword())) {
            return Result.error(String.format(EMPTY_INFO, "SSH连接密码"));
        }
        if (Objects.isNull(vmNode.getUsername())) {
            return Result.error(String.format(EMPTY_INFO, "SSH连接用户名"));
        }
        return vmNodeService.saveNode(vmNode);
    }

    @ApiOperation(value = "安装节点", notes = "安装节点")
    @PostMapping(value = "/init")
    public Result<String> installNode(@RequestBody String id) {
        return vmNodeService.initNode(id);
    }

    /**
     * 编辑
     *
     * @param vmNode 节点实例
     * @return 返回是否修改成功提示语
     */
    @AutoLog(value = "vm_node-编辑")
    @ApiOperation(value = "vm_node-编辑", notes = "vm_node-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody VmNode vmNode) {
        vmNodeService.updateById(vmNode);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "vm_node-通过id删除")
    @ApiOperation(value = "vm_node-通过id删除", notes = "vm_node-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        vmNodeService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "vm_node-批量删除")
    @ApiOperation(value = "vm_node-批量删除", notes = "vm_node-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.vmNodeService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "vm_node-通过id查询")
    @ApiOperation(value = "vm_node-通过id查询", notes = "vm_node-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<VmNode> queryById(@RequestParam(name = "id", required = true) String id) {
        VmNode vmNode = vmNodeService.getById(id);
        if (vmNode == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(vmNode);
    }


    @GetMapping(value = "/queryLoadDataById")
    public Result<VmNodeBasicData> queryLoadDataById(@RequestParam(name = "id", required = true) String id) {
        return vmNodeService.getLoadDataById(id);
    }

    @GetMapping(value = "/queryCpuLoadDataById")
    public Result<List<VmNodeCpuChartVo>> queryCpuLoadDataById(@RequestParam(name = "id", required = true) String id) {
        return vmNodeService.getCpuDataById(id);
    }

    @GetMapping(value = "/queryRamLoadDataById")
    public Result<List<VmNodeRamChartVo>> queryRamDataById(@RequestParam(name = "id", required = true) String id) {
        return vmNodeService.getRamDataById(id);
    }

    @GetMapping(value = "/queryDiskInfoById")
    public Result<List<VmNodeDiskChartVo>> queryDiskInfoById(@RequestParam(name = "id", required = true) String id) {
        return vmNodeService.getDiskDataById(id);
    }


    /**
     * 导出excel
     *
     * @param request
     * @param vmNode
     */
    @RequiresPermissions("vm:vm_node:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VmNode vmNode) {
        return null;
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return null;
    }

}
